home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / CurveGrader / Source / Manager.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.6 KB  |  152 lines

  1. /*
  2.  *    This class mostly does opening and saving.  Plus setting the matrices
  3.  *    and ScrollView up for max class size (45)
  4.  *    Rob Ferrante, 2/92
  5.  */
  6. #import "Manager.h"
  7. #import <appkit/Window.h>
  8. #import <appkit/ScrollView.h>
  9. #import <appkit/Matrix.h>
  10. #import <appkit/TextField.h>
  11. #import <appkit/OpenPanel.h>
  12. #import <streams/streams.h>
  13. #import <appkit/nextstd.h>
  14. #import "strings.h"
  15.  
  16. #define        FILESTAMP        "***   Produced By CurveGrader   ***"
  17. @implementation Manager
  18.  
  19. char  *titleFromString(char *str)
  20. {
  21.     static char  title[36];
  22.     int len = strlen(str);
  23.     
  24.     strcpy(title,"...");
  25.     if (len >32) strcpy(&title[3],&str[len -32]);
  26.     else strcpy(&title[3],str);
  27.     
  28.     return title;
  29. }
  30.  
  31. - init
  32. {
  33.     [super init];
  34.     currentPathName = NXCopyStringBuffer("~/UNTITLED");
  35.     return self;
  36. }
  37.  
  38. - saveToFileWithPanel:sender
  39. {
  40.     id  saver = [SavePanel new];
  41.     
  42.     if([saver runModal]==1) {
  43.         free( currentPathName);
  44.         currentPathName = NXCopyStringBuffer([saver filename]);
  45.         [quizWindow setTitle:titleFromString(currentPathName)];
  46.         [self saveToCurrentFile:self];
  47.     }
  48.     
  49.     return self;
  50. }
  51.  
  52. - readFromCurrentFile:sender
  53. {
  54.     NXStream *ms;
  55.     int i, rows, cols;
  56.     char  name[31],grade[5];
  57.     float score;
  58.     
  59.     if((ms =NXMapFile(currentPathName, NX_READONLY))==NULL) {
  60.         NXRunAlertPanel("Alert", "Couldn't read file", "OK", NULL, NULL);
  61.         return self;
  62.     }
  63.     
  64.  
  65.     [names getNumRows:&rows numCols:&cols];
  66.     name[0]='\0';
  67.     NXScanf(ms, "\"%[^\"]\"\n", name);
  68.     if (strcmp(name, FILESTAMP) != 0) {
  69.         NXRunAlertPanel("Alert", "Not a CurveGrader File", "OK", NULL, NULL);
  70.         return self;
  71.     }
  72.     name[0]='\0';
  73.     NXScanf(ms, "\"%[^\"]\"\n", name);
  74.     [classField setStringValue:name];
  75.     name[0]='\0';
  76.     NXScanf(ms, "\"%[^\"]\"\n", name);
  77.     [quizField setStringValue:name];
  78.     
  79.     for (i=0; i<rows; i++) {
  80.         name[0]='\0'; grade[0]='\0';score=0;
  81.         NXScanf(ms, "\"%[^\"]\",%f,\"%[^\"]\"\n", name, &score, grade);
  82.         [[names cellAt:i :0] setStringValue:name];
  83.         [[scores cellAt:i :0] setFloatValue:score];
  84.         [[grades cellAt:i :0] setStringValue:grade];
  85.     }
  86.     
  87.     NXCloseMemory(ms, NX_FREEBUFFER);
  88.     [quizWindow setTitle:titleFromString(currentPathName)];
  89.     
  90.     return self;
  91. }
  92.  
  93. - saveToCurrentFile:sender
  94. {
  95.     NXStream *ms;
  96.     int i, rows, cols;
  97.     
  98.     ms = NXMapFile(currentPathName, NX_WRITEONLY);    
  99.     if (ms == NULL) ms = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  100.  
  101.     [names getNumRows:&rows numCols:&cols];
  102.     
  103.     NXPrintf(ms, "\"%s\"\n", FILESTAMP);
  104.     NXPrintf(ms, "\"%s\"\n", [classField stringValue]);
  105.     NXPrintf(ms, "\"%s\"\n", [quizField stringValue]);
  106.     for (i=0; i<rows; i++)
  107.         NXPrintf(ms, "\"%s\",%f,\"%s\"\n",[[names cellAt:i :0] stringValue],
  108.                                           [[scores cellAt:i :0] floatValue],
  109.                                           [[grades cellAt:i :0] stringValue]);
  110.     if ( NXSaveToFile( ms, currentPathName)== -1)
  111.         NXRunAlertPanel("Alert", "Couldn't write file", "OK", NULL, NULL);
  112.     
  113.     NXCloseMemory(ms, NX_FREEBUFFER);
  114.     
  115.     return self;
  116. }
  117.  
  118. - readFromFileWithPanel:sender
  119. {
  120.     id  opener = [OpenPanel new];
  121.     
  122.     if([opener runModal]==1) {
  123.         free( currentPathName);
  124.         currentPathName = NXCopyStringBuffer([opener filename]);
  125.         [self readFromCurrentFile:self];
  126.     }
  127.     
  128.     return self;
  129. }
  130.  
  131. - appDidInit:sender
  132. {
  133.     NXRect  matrixRect, docRect, clipRect;
  134.     [names notifyAncestorWhenFrameChanged:YES];
  135.     [grades renewRows:45 cols:1];
  136.     [grades sizeToCells];
  137.     [scores renewRows:45 cols:1];
  138.     [scores sizeToCells];
  139.     [names renewRows:45 cols:1];
  140.     [names sizeToCells];
  141.     [names getBounds:&matrixRect];
  142.     [[scroller docView] getBounds:&docRect];
  143.     [[[scroller docView] superview] getBounds:&clipRect];
  144.     [[scroller docView] sizeTo:docRect.size.width :matrixRect.size.height+20];
  145.     [[scroller docView] moveTo:0 :(clipRect.size.height -docRect.size.height)];
  146.     [scroller reflectScroll:[[scroller docView] superview]];
  147.     [scroller display];
  148.     return self;
  149. }
  150.     
  151. @end
  152.